package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 子集 II
 * @author qulq
 * @date 2024-07-07 22:06:31
 */
public class T90_SubsetsIi{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T90_SubsetsIi().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<Integer>> ans = new ArrayList<>();
	List<Integer> path = new ArrayList<>();
	boolean[] used;

    public List<List<Integer>> subsetsWithDup(int[] nums) {
		Arrays.sort(nums);
		used = new boolean[nums.length];
		backtracking(nums, 0);
		return ans;
    }

	private void backtracking(int[] nums, int start) {

		ans.add(new ArrayList<>(path));

		for (int i = start; i < nums.length; i++) {
			if (i > 0 && nums[i - 1] == nums[i] && used[i - 1] == false) {
				continue;
			}
			path.add(nums[i]);
			used[i] = true;
			backtracking(nums, i + 1);
			path.remove(path.size() - 1);
			used[i] = false;
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}